import numpy as np
import cv2
import matplotlib.pyplot as plt
import os

'''
锐化
'''
class Sharpen():
    laplace_field_4 = 4 #拉普拉斯算子4领域模板
    laplace_field_8 = 8 #拉普拉斯算子8领域模板

    @staticmethod
    def sharpen():
        pass

    @staticmethod
    def robert(image):
        '''
        robert 算子, 对于一个像素点的值，用周围4个值交叉相减的绝对值之和代替。
        |f(i,j) - f(i+1, j+1)| + |f(i+1, j) - f(i, j+1)|
        '''
        h = image.shape[0]
        w = image.shape[1]
        image_new = np.zeros(image.shape, np.uint8)
        for i in range(1, h-1):
            for j in range(1, w-1):
                image_new[i][j] = np.abs(
                    image[i][j].astype(int) - image[i+1][j+1].astype(int)) + np.abs(
                        image[i+1][j].astype(int) - image[i][j+1].astype(int)
                    )
        return image_new


    @staticmethod
    def laplace(image, field = laplace_field_4):
        kernel = None
        if(field == Sharpen.laplace_field_4):               
            kernel = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]], dtype=int)
        elif field == Sharpen.laplace_field_8:
            kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]], dtype=int)

        img = cv2.filter2D(image, -1, kernel)
        return img
    
    @staticmethod
    def laplace_mask(image, field = laplace_field_4, alpha = 1.0):
        kernel = None
        if(field == Sharpen.laplace_field_4):               
            kernel = np.array([[0, -1, 0], [-1, 4 + alpha, -1], [0, -1, 0]])
        elif field == Sharpen.laplace_field_8:
            kernel = np.array([[-1, -1, -1], [-1, 8 + alpha, -1], [-1, -1, -1]])

        img = cv2.filter2D(image, -1, kernel)
        return img


'''sharpen test'''
def test_sharpen_time_domain():
    img = cv2.imread('./img/CBSD68/285079.png')
    img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # robert = Sharpen.robert(img_grey)
    # # lap_4 = Sharpen.laplace(img_grey)
    # lap_8 = Sharpen.laplace(img_grey, Sharpen.laplace_field_8)


    a0 = Sharpen.laplace_mask(img_grey, Sharpen.laplace_field_8, 0)
    a0_5 = Sharpen.laplace_mask(img_grey, Sharpen.laplace_field_8, 0.5)
    a1 = Sharpen.laplace_mask(img_grey, Sharpen.laplace_field_8, 1)
    a1_5 = Sharpen.laplace_mask(img_grey, Sharpen.laplace_field_8, 1.5)

    cv2.imshow("original", img)
    cv2.imshow("grey", img_grey)
    # cv2.imshow("robert", robert)
    # cv2.imshow("laplace_4", lap_4)
    # cv2.imshow("laplace_8", lap_8)
    cv2.imshow("a = 0", a0)
    cv2.imshow("a = 0.5", a0)
    cv2.imshow("a = 1", a1)
    cv2.imshow("a = 1.5", a1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# test_sharpen_time_domain()


class LinearEnhancement():
    @staticmethod
    def adjustGrayContrast(image, valve = 1.0):
        #获取图像高度和宽度
        height = image.shape[0]
        width = image.shape[1]
        #创建一幅图像
        result = np.zeros((height, width), np.uint8)

        #图像对比度增强变换
        for i in range(height):
            for j in range(width):
                
                if (int(image[i,j]* valve) > 255):
                    gray = 255
                else:
                    gray = int(image[i,j] * valve)
                    
                result[i,j] = np.uint8(gray)

        return result

    @staticmethod
    def moveUpDown(image, valve):
        #获取图像高度和宽度
        height = image.shape[0]
        width = image.shape[1]

        #创建一幅图像
        result = np.zeros((height, width), np.uint8)

        #图像灰度上移变换 DB=DA+50
        for i in range(height):
            for j in range(width):
                temp_value = int(image[i,j] + valve)

                if (temp_value) > 255:
                    gray = 255

                elif(temp_value < 0):
                    gray = 0

                else:
                    gray = temp_value
                    
                result[i,j] = np.uint8(gray)

        return result

    @staticmethod
    def reverse(image, value):
        #获取图像高度和宽度
        height = image.shape[0]
        width = image.shape[1]

        #创建一幅图像
        result = np.zeros((height, width), np.uint8)

        #图像灰度反色变换 DB=255-DA
        for i in range(height):
            for j in range(width):
                gray = 255 - image[i,j]
                result[i,j] = np.uint8(gray)

        return result


class NonLinearEnhancement():
    @staticmethod
    def logarithm(image, c):
        result = c * np.log(1.0 + image)
        result = np.uint8(result)
        return result

    @staticmethod
    def get_logarithm_curve(c):
        x = np.arange(0, 255)
        y = c * np.log(1.0 + x)
        plt.figure()
        plt.title("nonlinear_logarithm")
        # 表达的是x轴的刻度内容的范围
        plt.xticks((0, 255))
        # 表达的是y轴的刻度内容的范围
        plt.yticks((0, 255))
        plt.text(125, 1560, 'c='+str(c))
        plt.plot(x, y)

        #保存图片
        filepath = os.path.join("cache", 'lg_curve_c={}.png'.format(c))
        plt.savefig(filepath)

        return filepath


def equalizeHist(image):
    result = cv2.equalizeHist(image)
    return result

